from rest_framework.serializers import ModelSerializer, SerializerMethodField
from .models import *


class AdvSer(ModelSerializer):
    class Meta:
        model = AdvModel
        fields = '__all__'


class AdvCate2Ser(ModelSerializer):
    cate3 = SerializerMethodField()

    def get_cate3(self, obj):
        set = AdvModel.objects.filter(cate_id=obj.id)
        return AdvSer(set, many=True).data

    class Meta:
        model = AdvModel
        fields = '__all__'


class AdvCate1Ser(ModelSerializer):
    cate1 = SerializerMethodField()
    cate2 = SerializerMethodField()

    def get_cate1(self, obj):
        set = AdvModel.objects.filter(cate_id=obj.id)
        return AdvSer(set, many=True).data

    def get_cate2(self, obj):
        set = AdvModel.objects.filter(cate_id=obj.id)
        ser = []
        for i in set:
            set = AdvModel.objects.filter(cate_id=i.id)
            ser += AdvCate2Ser(set, many=True).data
        return ser

    class Meta:
        model = AdvModel
        fields = '__all__'


class TypeSer(ModelSerializer):
    class Meta:
        model = TypeModel
        fields = '__all__'
